package com.ljl.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljl.ssm.entity.CompleteIn;
import com.ljl.ssm.entity.User;
import com.ljl.ssm.mapper.CompleteInMapper;
import com.ljl.ssm.service.ICompleteInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;


@Service
public class CompleteInServiceImpl extends ServiceImpl<CompleteInMapper, CompleteIn> implements ICompleteInService {
    @Autowired
    CompleteInMapper completeInMapper;
    CompleteIn completeIn;
    HttpServletRequest request;
    @Override
    public CompleteIn userInfo(HttpServletRequest request,Model model) {
        //从前端session获取user信息
        User user=new User();
        user=(User) request.getSession().getAttribute("user");
        //通过查找userId锁定要查看的用户详情
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",user.getId());
        CompleteIn completeIn=completeInMapper.selectOne(queryWrapper);
        System.out.println("调出的数据："+completeIn);
        //存到session中方便信息更新
        request.getSession().setAttribute("completeIn",completeIn);
      //  将取出的信息传递到前端
        model.addAttribute("password",user.getPassword());
        model.addAttribute("picture",completeIn.getPicture());
        model.addAttribute("nick",completeIn.getNick());
        model.addAttribute("address",completeIn.getAddress());
        model.addAttribute("eMail",completeIn.geteMail());
        model.addAttribute("postCode",completeIn.getPostCode());
        model.addAttribute("tel",completeIn.getTel());
        model.addAttribute("trueName",completeIn.getTrueName());
       return completeIn;
    }

    @Override
    public CompleteIn editUserInfo(HttpServletRequest request, CompleteIn completeIn, MultipartFile pic,Model model) {
        //给图片随机取一个名字(含后缀)
        String picName=getFileName();
        //将图片存入仓库
        try {
            //将图片保存到NGINX仓库
            pic.transferTo(new File("D:/pic/"+picName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //拼接存入数据库picture的路径，并设置picture属性
        completeIn.setPicture(picName);
        //工具人completeIn1出现啦
        CompleteIn completeIn1=new CompleteIn();
        completeIn1=(CompleteIn) request.getSession().getAttribute("completeIn");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",completeIn1.getUserId());
        int result=completeInMapper.update(completeIn,queryWrapper);
        //将用户填写的信息显示在页面上
        //工具人2号,目的是为了更新对象里的信息
        CompleteIn completeIn2=new CompleteIn();
        completeIn2=completeInMapper.selectById(completeIn1.getId());
        model.addAttribute("picture",completeIn2.getPicture());
        model.addAttribute("nick",completeIn2.getNick());
        model.addAttribute("address",completeIn2.getAddress());
        model.addAttribute("eMail",completeIn2.geteMail());
        model.addAttribute("postCode",completeIn2.getPostCode());
        model.addAttribute("tel",completeIn2.getTel());
        model.addAttribute("trueName",completeIn2.getTrueName());
        return completeIn;
    }
    private String getFileName() {
        String dateStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS"));
        String rnd = ""+new Random().nextInt(10);
        return dateStr+rnd+".png";
    }



}
